<template>
    <div class="content">
        <!-- 顶部选项卡 -->
        <scroll-view
            id="nav-bar"
            class="nav-bar"
            scroll-x
            scroll-with-animation
            :scroll-left="scrollLeft"
        >
            <div
                v-for="(item, index) in tabs"
                :key="item.id"
                :id="'tab' + index"
                class="nav-item"
                :class="{ current: index === tabCurrentIndex }"
                @click="changeTab(index, item)"
            >
                {{ item.name }}
            </div>
        </scroll-view>
    </div>
</template>
<script setup lang="ts">
import { defineProps, withDefaults, ref } from 'vue'
interface ITabs {
    id: Number
    name: string
}
interface IProps {
    tabs?: ITabs[] | any
    onChange: Function
}
const props = withDefaults(defineProps<IProps>(), {
    tabs: [],
    onChange: () => {}
})
const tabCurrentIndex = ref<Number>(0)
const scrollLeft = 0

const changeTab = (index: Number, item: IProps) => {
    tabCurrentIndex.value = index
    props.onChange(item)
}
</script>
<style lang="scss" scoped>
.content {
    background-color: #f8f8f8;
    height: 100%;
    overflow: hidden;
}

/* 隐藏滚动条scrollbar */
.nav-bar ::-webkit-scrollbar {
    display: none;
    background-color: transparent;
}

/* 顶部tabbar */
.nav-bar {
    position: relative;
    z-index: 10;
    height: 90upx;
    white-space: nowrap;
    box-shadow: 0 2upx 8upx rgba(0, 0, 0, 0.06);
    background-color: #fff;

    .nav-item {
        display: inline-block;
        width: 150upx;
        height: 90upx;
        text-align: center;
        line-height: 90upx;
        font-size: 30upx;
        color: #303133;
        position: relative;

        &:after {
            content: '';
            width: 0;
            height: 0;
            border-bottom: 4upx solid #007aff;
            position: absolute;
            left: 50%;
            bottom: 0;
            transform: translateX(-50%);
            transition: 0.3s;
        }
    }

    .current {
        color: #007aff;

        &:after {
            width: 50%;
        }
    }
}
</style>
